<?php
include_once '../comic/WCPersistantObject.php';
include_once '../utils/WCRelation.php';

/*
 * Copyright 2009 Dan Klco
 * This file is part of Daily WebComics.
 * 
 * Daily WebComics is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Daily WebComics is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Daily WebComics.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * This class represents a single comic 'strip' or collection of comics.  It contains links
 * to the authors of the strip, the comics objects that represent each comic in the collection (man I need 
 * to think of a better term) and the configuration object.
 * 
 * @author daniel.klco
 * @version 20090303
 */
class WCComicStrip extends WCPersistantObject{
	public static $ATTR_NAME='name';
	public static $ATTR_HOMEPAGE='homepage';
	public static $ATTR_RELEASE_SCHEDULE='release_schedule';
	public static $ATTR_MOST_RECENT='most_recent';
	public static $ATTR_RSS_FEED='rss_feed';
	public static $ATTR_RANDOM_COMIC_URL='random_comic_url';
	
	/**
	 * Creates a new author-comic relationship between this object and the specified author.
	 * 
	 * @param $author
	 * @param $db
	 * @return none
	 */
	public function addAuthor($author){
		$this->createRelationship($author,WCRelation::$AUTHOR_COMIC_RELATION);
	}
	
	/**
	 * Creates a new comic strip - comic relationship between this object and the specified comic.
	 * 
	 * @param $comic
	 * @param $db
	 * @return none
	 */
	public function addComic($comic){
		$this->createRelationship($comic,WCRelation::$COMIC_STRIP_COMIC_RELATION);
	}
	
	/**
	 * Returns true if the object can be saved to the database.
	 * 
	 * @param true if the object is new eg. going to be inserted, false if it is going to be updated
	 * @return true if the object can be saved, false otherwise
	 */
	protected function canSave($new){
		if(!$new){
			$cond = ' AND NOT id='.$this->getId();
		}
		$query = $this->db->prepare('SELECT 1 FROM '.$this->table.' '.WCComicStrip::$ATTR_HOMEPAGE.'=\'?\''.$cond,array($this->getAttribute(WCComicStrip::$ATTR_HOMEPAGE)));
		$result = $this->db->executeQuery($query);
		if(mysql_num_rows($result) != 0){
			return false;
		}
		return true;
	}
	
	/**
	 * Builds a new WCComicStrip object
	 * 
	 * @param $db
	 * @param $id
	 * @return unknown_type
	 */
	public function __construct($db,$id=NULL){
		parent::__construct('wc_comic_strip',$db,$id);
	}
	
	/**
	 * Creates the table in the database for this object.  Passing in the delete flag as true will cause the function to
	 * delete the table if it already exists.  If this flag is false or omitted the function will throw an error if the 
	 * table already exists.
	 * 
	 * The columns for WCComic are:
	 * 	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
	 * 	name VARCHAR(512)
	 * 	homepage VARCHAR(512)
	 *  release_schedule VARCHAR(52)
	 *  most_recent INT
	 *  rss_feed VARCHAR(512)
	 *  random_comic_url VARCHAR(512)
	 * 
	 * @param $delete if true the table will be deleted if it already exists
	 * @return none
	 */
	public function createTable($delete=false){
		parent::createTable($delete);
		$this->db->executeQuery('CREATE TABLE '.$this->table.' (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,title VARCHAR(512),description TEXT,src VARCHAR(512),alt VARCHAR(512))'); 
	}
	
	/**
	 * Returns a list of the of the comics the strip is associated with.
	 * 
	 * @return list of comic ids
	 */
	public function getAuthorIds(){
		return array_keys($this->getRelated(WCRelation::$AUTHOR_COMIC_RELATION));
	}
	
	/**
	 * Returns a list of the of the comics the strip is associated with.
	 * 
	 * @return list of comic ids
	 */
	public function getComicIds(){
		return array_keys($this->getRelated(WCRelation::$COMIC_STRIP_COMIC_RELATION));
	}
	
	/**
	 * Returns all of the accepted attributes for the object.  These attributes will be saved to the database
	 * when the object is saved.  The key is the name of the attribute the value is whether or not it requires
	 * quotes.
	 * 
	 * The acceptable attributes for WCComicStrip are:
	 *  name
	 *  homepage
	 *  release_schedule
	 *  most_recent
	 *  rss_feed
	 *  random_comic_url
	 * 
	 * @return An array of the attributes for the object.
	 */
	public function objectAttributes(){
		return array('name'=>true,'homepage'=>true,'release_schedule'=>true,'most_recent'=>false,'rss_feed'=>true,'random_comic_url'=>true);	
	}
	
	/**
	 * Returns all of the required attributes for the object.  If these attributes are not set, the object
	 * will throw an exception while attempting to save.
	 * 
	 * The required attributes for WCComic are:
	 * 	name
	 *  homepage
	 *  release_schedule
	 * 
	 * @return An array of the required attributes for the object.
	 */
	public function requiredAttributes(){
		return array('name','homepage','release_schedule');
	}
}
?>
